package com.idez.wipservice;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private Controller controller;  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        controller = new Controller();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String login = request.getParameter("login");
		String senha = request.getParameter("senha");
		
		String json = null;
		
		if (controller.verificaLogin(login, senha)) {
			String papelUsuario = controller.verificaPapelUsuario(login); 
			if(papelUsuario.equals(controller.VENDEDOR)){
				json = "{\"response\": { \"valor\" : \"OK\" }, "
						+ "\"itens\" : [ {\"id\" : \"Gerar Contrato\" }, {\"id\" : \"Visualizar Roteiro\" }"
						+ ", {\"id\" : \"Imprimir Recibo\"}, {\"id\" : \"Visualizar Mapa\" }, ]}";
			}else {
				json = "{\"response\": { \"valor\" : \"OK\" }, "
						+ "\"itens\" : [ {\"id\" : \"Baixar OS\" }, {\"id\" : \"Visualizar Roteiro\" }"
						+ ", {\"id\" : \"Imprimir Recibo\"}, {\"id\" : \"Visualizar Mapa\" } , ]}";
			}
			
		} else {
			json = "{\"response\": { \"valor\" : \"ERROR\" } "
					+ "}";
		}
		response.getWriter().write(json);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
